package com.tulele.vtool.task.async;

import com.tulele.vtool.bus.mapper.log.PushDetailLogMapper;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.log.PushDetailLog;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.SpringContextUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

//佣金抵扣推送
@Component
@Slf4j
public class CommissionDeductionMsgAsyncTask {

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private IUserService userService;

//    @Value("${openMemberMenuUrl}")
//    private String openMemberMenuUrl;
//
//    @Value("${commissionDeductionTempID}")
//    private String commissionDeductionTempID;
//
//    @Value("${articleListMenuUrl}")
//    private String articleListMenuUrl;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;


    @Async("taskExecutor")
    public void execute(UserInfo user, Integer headId,String profile) {

        Integer ret = 2;

        if (user.getOpenId() != null) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            // 发送模板消息接口
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    // 接收者openid
                    .toUser(user.getOpenId())
                    // 模板id
                    .templateId(sysGzh.getSysGzh().getCommissionDeductionTempId())
                    // 模板跳转链接
                    .url(sysGzh.getOpenMemberMenuUrl()+"?mt=1")
                    .build();

            String firstMsg =  "佣金最新通知";
            String remarkMsg = "\uD83D\uDC49 点击查看自己的奖励金";

            // 添加模板数据
            templateMessage.addData(new WxMpTemplateData("first", firstMsg, "#FF00FF"))
                    .addData(new WxMpTemplateData("keyword1","-","#A9A9A9" ))
                    .addData(new WxMpTemplateData("keyword2", "-", "#A9A9A9"))
                    .addData(new WxMpTemplateData("keyword3", "亲！您的账号已经有 "+user.getAccountBalance().toString()+" 元奖励金，奖励金可用作抵扣会员费哦\uD83D\uDE0D", "#FF0000"))
                    .addData(new WxMpTemplateData("keyword4", "用奖励金抵扣会员费太优惠了~", "#A9A9A9"))
                    .addData(new WxMpTemplateData("keyword5", user.getAccountBalance().toString(), "#A9A9A9"))
                    .addData(new WxMpTemplateData("remark", remarkMsg, "#0000FF"));

            PushDetailLog pushDetailLog = new PushDetailLog();
            pushDetailLog.setGzhCode(profile);
            pushDetailLog.setUserId(user.getUserId());
            pushDetailLog.setMsgType(13);//佣金抵扣
            pushDetailLog.setPushTime(new Date());
            pushDetailLog.setHeadId(headId);
            pushDetailLog.setOpenId(user.getOpenId());

            try {
                WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(profile);
                if(weixinService==null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile,weixinService);
                }
                weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                ret = 0;
                pushDetailLog.setMsg("成功");
            } catch (WxErrorException e) {
                ret = 2;
                pushDetailLog.setMsg(e.getMessage());
            }

            pushDetailLog.setResult(ret);

            pushDetailLogMapper.addPushDetailLog(pushDetailLog);

        }
    }
}
